ifeq (${TARGET},WEB)
	CC = emcc
	CXX = em++
	SUF = html
else
	CC = gcc
	CXX = g++
	SUF = out
endif

SRCDIR = ./src

CSRCS = $(wildcard ./src/*.c)
COBJS = $(patsubst %.c,%.o,$(CSRCS))

CXXSRCS = $(wildcard ./src/*.cpp)
CXXOBJS = $(patsubst %.cpp,%.o,$(CXXSRCS))

TESTSRCDIR = ./test

TCSRCS = $(wildcard ./test/*.c)
TCTARGET = $(patsubst %.c,%.html,$(TCSRCS))

TCXXSRCS = $(wildcard ./test/*.cpp)
TCXXTARGET = $(patsubst %.cpp,%.html,$(TCXXSRCS))

ifeq (${TARGET},WEB)
	TOUT = ./bin/game.html
else
	TOUT = ./bin/game.out
endif

OUTPATH = ./bin/
CFLAGS = -L./lib/ -I./include -I/usr/lib/emscript/include -O3
CXXFLAGS = -std=c++20
ifeq (${TARGET},WEB)
	CFLAGS := ${CFLAGS} -s USE_GLFW=3 -s ASYNCIFY -DPLATFORM_WEB --preload-file ./resource -sALLOW_MEMORY_GROWTH -sABORTING_MALLOC=0 --shell-file ./include/minshell.html
endif
ifeq (${TARGET},WEB)
CLINK = -lwraylib
else
CLINK = -lraylib
endif
CXXLINK =

RMFI = $(foreach x,$(COBJS) ${CXXOBJS} ${TCTARGET} ${TCXXTARGET},$(wildcard ${x}))

.PHNOY:clean

build : $(COBJS) $(CXXOBJS)
	${CXX} ${COBJS} ${CXXOBJS} ${CFLAGS} ${CLINK} ${CXXLINK} -o ${TOUT}

$(SRCDIR)/%.o : $(SRCDIR)/%.c
	${CC} -c $< ${CFLAGS} ${CLINK} -o $@

$(SRCDIR)/%.o : $(SRCDIR)/%.cpp
	${CXX} -c $< ${CFLAGS} ${CXXFLAGS} ${CLINK} ${CXXLINK} -o $@

clean:
ifneq (${RMFI},)
	rm -rf ${RMFI}
endif

_test: $(TCTARGET) $(TCXXTARGET)
	@echo "build to ${TCTARGET} ${TCXXTARGET}"

$(TESTSRCDIR)/%.html : $(TESTSRCDIR)/%.c
	${CC} $< ${CFLAGS} ${CLINK} -o $@

$(TESTSRCDIR)/%.html : $(TESTSRCDIR)/%.cpp
	${CXX} $< ${CFLAGS} ${CLINK} -o $@
